package link.ten_fei.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import link.ten_fei.Bean.Pl_List;
import link.ten_fei.service.AddMRService;
import link.ten_fei.service.AddMRServiceImpl;

/**
 * Servlet implementation class AddMRServlet
 */
@WebServlet("/addMR.action")
public class AddMRServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddMRServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置前端页面传递回来的字符编码
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		String id=request.getParameter("id");
		AddMRService addMRService = new AddMRServiceImpl();
		if (action == null) {
			// 跳转到添加页面
			request.setAttribute("mainRight", "/WEB-INF/jsp/addMR.jsp");
			request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
		} else if (action != null && action.equals("add")) {
			// 添加病历
			String pl_number = request.getParameter("pl_number");
			String pl_date = request.getParameter("pl_date");
			String pl_name = request.getParameter("pl_name");
			String pl_sex = request.getParameter("pl_sex");
			String pl_age = request.getParameter("pl_age");
			String pl_expensetype = request.getParameter("pl_expensetype");
			String pl_diagnosis = request.getParameter("pl_diagnosis");
			String pl_department = request.getParameter("pl_department");
			String pl_rp = request.getParameter("pl_rp");
			String pl_doctor=request.getParameter("doc_name");
			String pl_deployment = request.getParameter("pl_deployment");
			String pl_review = request.getParameter("pl_review");
			String pl_amount = request.getParameter("pl_amount");
			Pl_List list = new Pl_List();
			list.setPl_number(pl_number);
			list.setPl_date(pl_date);
			list.setPl_name(pl_name);
			list.setPl_sex(pl_sex);
			list.setPl_age(pl_age);
			list.setPl_expensetype(pl_expensetype);
			list.setPl_diagnosis(pl_diagnosis);
			list.setPl_department(pl_department);
			list.setPl_rp(pl_rp);
			list.setPl_doctor(pl_doctor);
			list.setPl_deployment(pl_deployment);
			list.setPl_review(pl_review);
			list.setPl_amount(pl_amount);
			// 将该对象添加到数据库中
			int value= addMRService.save(list);
			// 提示信息
			request.setAttribute("success_add", value);
			// 添加成功，跳转到添加页面
			request.setAttribute("mainRight", "/WEB-INF/jsp/addMR.jsp");
			request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
		} else if (action != null && action.equals("modify")) {
			// 更改病历
			String pl_number = request.getParameter("pl_number");
			String pl_date = request.getParameter("pl_date");
			String pl_name = request.getParameter("pl_name");
			String pl_sex = request.getParameter("pl_sex");
			String pl_age = request.getParameter("pl_age");
			String pl_expensetype = request.getParameter("pl_expensetype");
			String pl_diagnosis = request.getParameter("pl_diagnosis");
			String pl_department = request.getParameter("pl_department");
			String pl_rp = request.getParameter("pl_rp");
			String pl_doctor = request.getParameter("pl_doctor");
			String pl_deployment = request.getParameter("pl_deployment");
			String pl_review = request.getParameter("pl_review");
			String pl_amount = request.getParameter("pl_amount");

			Pl_List list = new Pl_List();
			list.setPl_number(pl_number);
			list.setPl_date(pl_date);
			list.setPl_name(pl_name);
			list.setPl_sex(pl_sex);
			list.setPl_age(pl_age);
			list.setPl_expensetype(pl_expensetype);
			list.setPl_diagnosis(pl_diagnosis);
			list.setPl_department(pl_department);
			list.setPl_rp(pl_rp);
			list.setPl_doctor(pl_doctor);
			list.setPl_deployment(pl_deployment);
			list.setPl_review(pl_review);
			list.setPl_amount(pl_amount);
			// 将要更改的对象保存到数据库
			int flag=addMRService.change(list,Integer.parseInt(id));
			
			request.setAttribute("success_modify", flag);
			// 添加成功，跳转到添加页面
			request.setAttribute("mainRight", "/WEB-INF/jsp/addMR.jsp");
			request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
		}

	}

}
